package cn.com.xiaoshoujia.activities.service.impl;

import cn.com.xiaoshoujia.activities.activity.ActivityOperationActivity;
import cn.com.xiaoshoujia.activities.dto.ActivityOperationAddDTO;
import cn.com.xiaoshoujia.activities.dto.ActivityOperationDTO;
import cn.com.xiaoshoujia.activities.dto.ActivityOperationQueryDTO;
import cn.com.xiaoshoujia.activities.entity.ActivityOperation;
import cn.com.xiaoshoujia.activities.service.ActivityOperationService;
import cn.com.xiaoshoujia.company.entity.CompanyLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author dulei
 * @Title: ActivityOperationServiceImpl
 * @Description: 团购设置Service实现类
 * @Time 2017/7/27 14:47
 */
@Service
public class ActivityOperationServiceImpl implements ActivityOperationService {

    private Logger logger = LoggerFactory.getLogger(ActivityOperationServiceImpl.class);

    @Autowired
    ActivityOperationActivity activityOperationActivity;

    /**
     * @Author: dulei
     * @Description: 查询团购活动设置信息
     * @param: xcode 公司唯一XCODE
     * @Date: 2017/7/27 14:55
     */
    @Override
    public ActivityOperationDTO findOperation(String xcode) {

        return activityOperationActivity.findOperation(xcode);
    }

    /**
     * @Author: dulei
     * @Description: 设置拼团，回显数据
     * @param: xcode 公司唯一xcode
     * @Date: 2017/7/28 9:39
     */
    @Override
    public ActivityOperationQueryDTO findTeamOperation(String xcode) {
        return activityOperationActivity.findTeamOperation(xcode);
    }

    /**
     * @param operationAddDTO 参数包装类
     * @param com             企业类
     * @Author: dulei
     * @Description:
     * @param: * @param null
     * @Date: 2017/7/28 13:48
     */
    @Override
    @Transactional
    public boolean saveOrUpdateOperation(ActivityOperationAddDTO operationAddDTO, CompanyLogin com) throws RuntimeException {
        ActivityOperation activityOperation = activityOperationActivity.findByCompanyXcode(com.getXcode());

        //企业为保存过团购活动设置
        if (null == activityOperation) {
            logger.debug("企业第一次设置");
            boolean save = activityOperationActivity.saveOperation(operationAddDTO, com);
            return save;
        }
        //企业更新设置
        logger.debug("企业更新设置");
        boolean update = activityOperationActivity.updateOperation(operationAddDTO, com);
        return update;
    }
}
